package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.PersonScore;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by wu on 2016/4/22.
 * 奖惩积分填报查询
 */
public class PersonalScoreSearchFragment extends Fragment implements View.OnClickListener {

  protected static final String REQUEST_SEARCH =
      "com.isunland.managesystem.ui.PersonalScoreSearchFragment.REQUEST_SEARCH";
  public static final String REQUEST_START =
      "com.isunland.managesystem.ui.PersonalScoreSearchFragment.REQUEST_START";
  public static final String REQUEST_END =
      "com.isunland.managesystem.ui.PersonalScoreSearchFragment.REQUEST_END";
  public static final String REQUEST_DATASTATUS =
      "com.isunland.managesystem.ui.PersonalScoreSearchFragment.REQUEST_DATASTATUS";
  private static final int REQUEST_STARTTIME = 0;
  private static final int REQUEST_FINISHTIME = 1;
  private static final int REQUEST_DATA_STATUS = 2;
  public static final String VALUE_ALL = "";
  public static final String VALUE_NEW = "new";
  public static final String VALUE_SUBMIT = "submit";
  public static final String VALUE_CHECKPASS = "publish";
  public static final String VALUE_BACK = "newback";
  LinearLayout mllifPay;
  TextView mtv_startTime_workConvert;// 开始时间
  TextView mtv_endTime_workConvert;// 结束时间
  private TextView mtv_dataState_delivery;//数据状态
  private String mStartTime;
  private String mEndTime;
  PersonScore mScoreSearch = new PersonScore();
  private String mDataStatus;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.sift);
    mStartTime = getArguments().getString(REQUEST_START);
    String end = getArguments().getString(REQUEST_END);
    if (!TextUtils.isEmpty(mEndTime)) {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
      try {
        Date date = sdf.parse(end);
        mEndTime = MyDateUtil.dateToString(MyDateUtil.getAfterDay(date, 1), MyDateUtil.TYPE_DATE);
      } catch (ParseException e) {
        e.printStackTrace();
      }
    }
    mDataStatus = getArguments().getString(REQUEST_DATASTATUS);
    LogUtil.i("mStartTime===="
        + mStartTime
        + ",,mEndTime===="
        + mEndTime
        + ",,mDataStatus===="
        + mDataStatus);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_delivery_query, container, false);
    mtv_startTime_workConvert = (TextView) view.findViewById(R.id.tv_startDate_delivery);
    mtv_endTime_workConvert = (TextView) view.findViewById(R.id.tv_endDate_delivery);
    mtv_dataState_delivery = (TextView) view.findViewById(R.id.tv_dataState_delivery);
    mllifPay = (LinearLayout) view.findViewById(R.id.ll_ifPay);

    mllifPay.setVisibility(View.GONE);

    putDataStatus();

    if (!TextUtils.isEmpty(mStartTime)) {
      mtv_startTime_workConvert.setText(mStartTime);
    } else {
      mtv_startTime_workConvert.setText(MyDateUtil.getFirstDay());
    }

    if (!TextUtils.isEmpty(mEndTime)) {
      mtv_endTime_workConvert.setText(mEndTime);
    } else {
      mtv_endTime_workConvert.setText(MyDateUtil.getLastDay());
    }

    mtv_startTime_workConvert.setOnClickListener(this);
    mtv_endTime_workConvert.setOnClickListener(this);
    mtv_dataState_delivery.setOnClickListener(this);
    return view;
  }

  /**
   * 设置根据数据状态
   */
  private void putDataStatus() {
    if (!TextUtils.isEmpty(mDataStatus)) {
      if (mDataStatus.equalsIgnoreCase(VALUE_ALL)) {
        mtv_dataState_delivery.setText(R.string.all);
      } else if (mDataStatus.equalsIgnoreCase(VALUE_NEW)) {
        mtv_dataState_delivery.setText(R.string.draft);
      } else if (mDataStatus.equalsIgnoreCase(VALUE_SUBMIT)) {
        mtv_dataState_delivery.setText(R.string.alreadySubmit);
      } else if (mDataStatus.equalsIgnoreCase(VALUE_CHECKPASS)) {
        mtv_dataState_delivery.setText(R.string.alreadyPublish);
      } else if (mDataStatus.equalsIgnoreCase(VALUE_BACK)) {
        mtv_dataState_delivery.setText(R.string.alreadyNewBack);
      }
    } else {
      mtv_dataState_delivery.setText(R.string.all);
    }
  }

  /**
   * 获取时间
   */
  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 开始时间
      case R.id.tv_startDate_delivery:
        showDialog(REQUEST_STARTTIME, MyDateUtil.stringToDate(mStartTime, MyDateUtil.TYPE_DATE));
        break;
      // 结束时间
      case R.id.tv_endDate_delivery:
        showDialog(REQUEST_FINISHTIME, MyDateUtil.stringToDate(mEndTime, MyDateUtil.TYPE_DATE));
        break;
      case R.id.tv_dataState_delivery:
        showDialog(REQUEST_DATA_STATUS, null);
        break;

      default:
        break;
    }
  }

  /**
   * 显示时间对话框
   *
   * @param requestCode 请求码
   * @param date 对话框初始显示时间
   */
  public void showDialog(int requestCode, Date date) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_STARTTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;
      case REQUEST_FINISHTIME:
        dialog = BaseYMDTimeDialogFragment.newInstance(date, 0);
        break;
      case REQUEST_DATA_STATUS:
        dialog = new ScoreSearchDialogFragment();
        break;
      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    if (dialog != null) {
      dialog.setTargetFragment(PersonalScoreSearchFragment.this, requestCode);
    }
    //noinspection ConstantConditions
    dialog.show(fm, "");
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回按钮

      case android.R.id.home:
        getActivity().finish();
        break;

      case R.id.menu_item_confirm:
        String startTime = mtv_startTime_workConvert.getText().toString();
        String endTime = mtv_endTime_workConvert.getText().toString();
        String dataStatus = mtv_dataState_delivery.getText().toString();
        String status = "";
        if (dataStatus.equalsIgnoreCase(getResources().getString(R.string.all))) {
          status = VALUE_ALL;
        } else if (dataStatus.equalsIgnoreCase(getResources().getString(R.string.draft))) {
          status = VALUE_NEW;
        } else if (dataStatus.equalsIgnoreCase(getResources().getString(R.string.alreadySubmit))) {
          status = VALUE_SUBMIT;
        } else if (dataStatus.equalsIgnoreCase(getResources().getString(R.string.alreadyPublish))) {
          status = VALUE_CHECKPASS;
        } else if (dataStatus.equalsIgnoreCase(getResources().getString(R.string.alreadyNewBack))) {
          status = VALUE_BACK;
        }
        mScoreSearch.setStartDate(startTime);
        mScoreSearch.setEndDate(endTime);
        mScoreSearch.setDataStatus(status);
        boolean startBeforeEnd = MyDateUtil.startBeforeEnd(startTime, endTime);

        if (!startBeforeEnd) {
          Toast.makeText(getActivity(), R.string.startTimeBeforeEndTime, Toast.LENGTH_SHORT).show();
        } else {
          Intent intent = new Intent();
          intent.putExtra(REQUEST_SEARCH, mScoreSearch);
          getActivity().setResult(Activity.RESULT_OK, intent);
          getActivity().finish();
        }
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mtv_startTime_workConvert.setText(mStartTime);
      return;
    }

    if (requestCode == REQUEST_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      String endDate = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mEndTime =
          MyDateUtil.dateToString(MyDateUtil.getAfterDay(acquireDate, 1), MyDateUtil.TYPE_DATE);
      mtv_endTime_workConvert.setText(endDate);
      return;
    }

    if (requestCode == REQUEST_DATA_STATUS && data != null) {
      String dataStatus = data.getStringExtra(ScoreSearchDialogFragment.EXTRA_DATA_STATUS);
      if (dataStatus.equalsIgnoreCase(VALUE_ALL)) {
        mtv_dataState_delivery.setText(R.string.all);
      } else if (dataStatus.equalsIgnoreCase(VALUE_NEW)) {
        mtv_dataState_delivery.setText(R.string.draft);
      } else if (dataStatus.equalsIgnoreCase(VALUE_SUBMIT)) {
        mtv_dataState_delivery.setText(R.string.alreadySubmit);
      } else if (dataStatus.equalsIgnoreCase(VALUE_CHECKPASS)) {
        mtv_dataState_delivery.setText(R.string.alreadyPublish);
      } else if (dataStatus.equalsIgnoreCase(VALUE_BACK)) {
        mtv_dataState_delivery.setText(R.string.alreadyNewBack);
      }

      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static Fragment newInstance(String start, String end, String status) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(REQUEST_START, start);
    bundle.putSerializable(REQUEST_END, end);
    bundle.putSerializable(REQUEST_DATASTATUS, status);
    PersonalScoreSearchFragment fragment = new PersonalScoreSearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
